<?php

namespace App\Kuafu\Base\Service;

use App\Kuafu\Base\Enum\BaseCacheEnum;
use App\Kuafu\Base\Enum\BaseCodeEnum;
use App\Kuafu\Shop\Service\ShopService;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Support\Facades\Redis;

class BaseService
{
    /**
     * @var int brandId
     */
    public $brandId;

    /**
     * @var int shopId
     */
    public $shopId;

    public function __construct($brandId = 0, $shopId = 0)
    {
        if (empty($brandId)) {
            $brandId = ShopService::getShopInfo($shopId)->brandId ?? 0;
        }

        $this->brandId = $brandId;
        $this->shopId  = $shopId;
    }

    /**
     * 通用返回
     *
     * @param int $code
     * @param string $msg
     * @param array|null $data
     * @return array
     */
    public static function returnData(int $code, string $msg = '', array $data = null): array
    {
        if (!$msg && $code != 1) {
            $msg = BaseCodeEnum::getCodeTextAutoClass($code);
        }

        return ['code' => $code, 'msg' => $msg, 'data' => $data ?: null];
    }

    public static function getModuleByNameSpace($class)
    {
        $resModule = 'Base';

        // 通过调用该方法的Class，来判断模块
        $arr = explode('\\', $class ?: static::class);
        foreach ($arr as $k => $v) {
            if ($v == 'Kuafu') {
                $resModule = $arr[$k + 1] ?? 'Base';
            }
        }

        return $resModule;
    }

    /**
     * 生成唯一的自增数
     * @param int $length
     * @return string
     */
    public static function createIncrNo(int $length = 6): string
    {
        $autoIncr = Redis::incr(BaseCacheEnum::getIncrNo(), 1);
        if ($autoIncr == 900000) {
            Redis::set(BaseCacheEnum::getIncrNo(), 1);
        }

        return sprintf("%0{$length}s", $autoIncr);
    }

    /**
     * 获取以当前时间为基准的上个月开始时间到结束时间的时间戳
     * @return array
     */
    public static function getMonthStartToEnd(): array
    {
        return [
            strtotime(date('Y-m-1',strtotime('last month'))),
            strtotime(date('Y-m-d 23:59:59',strtotime(date('Y-m-1').'-1 day')))
        ];
    }

    /**
     * 获取当前月开始时间到结束时间的时间戳
     * @return array
     */
    public static function getNowMonthStartToEnd(): array
    {
        return [
            strtotime(date('Y-m-1')),
            strtotime(date('Y-m-d 23:59:59',strtotime(date('Y-m-1',strtotime('next month')).'-1 day')))
        ];
    }

    /**
     * 获取当前月开始时间到结束时间每天的时间戳
     * @return array
     */
    public static function getNowTimeDayToDayByMonth()
    {
        //目前天数
        $count = date('d');
        $timeSlot = [];
        for ($i = 1; $i <= $count; $i ++){
            $timeSlot[] = [strtotime(date('Y-m-'.$i.' 00:00:00')),strtotime(date('Y-m-'.$i.' 23:59:59'))];
        }
        return $timeSlot;
    }

    /**
     * 获取月的时间戳
     * @return array
     */
    public static function getMonthTime($date = null, $today = 0)
    {
        if(((string)$date) == '') {
            if($today == 0) {
                return [
                    'time'  => [strtotime(date('Y-m-1')), strtotime(date('Y-m-d', strtotime('-1day'))) + 86399],
                    'count' => (int)date('d') - 1
                ];
            } else {
                return [
                    'time'  => [strtotime(date('Y-m-1')), strtotime(date('Y-m-d') . '23:59:59')],
                    'count' => (int)date('d')
                ];
            }
        } else {
            //当月总天数
            $endDay = date('t', strtotime($date));
            $startTime = strtotime(date('Y-m-1', strtotime($date)));
            $endTime = strtotime(date('Y-m-' . $endDay . ' 23:59:59', strtotime($date)));
            if($endTime < time()) {
                return [
                    'time'  => [$startTime, $endTime],
                    'count' => date('t', strtotime($date))
                ];
            } else {
                //本月&未来
                if($startTime < time()) {
                    //本月
                    if($today == 0) {
                        return [
                            'time'  => [strtotime(date('Y-m-1')), strtotime(date('Y-m-d', strtotime('-1day'))) + 86399],
                            'count' => (int)date('d') - 1
                        ];
                    } else {
                        return [
                            'time'  => [strtotime(date('Y-m-1')), strtotime(date('Y-m-d') . '23:59:59')],
                            'count' => (int)date('d')
                        ];
                    }
                } else {
                    return [
                        'time'  => [],
                        'count' => 0
                    ];
                }
            }
        }
    }

    /**
     * 标准分页返回
     * @param LengthAwarePaginator $paginator
     * @param \Closure $closure
     * @return array
     */
    public static function pageLists(LengthAwarePaginator $paginator, \Closure $closure): array
    {
        return [
            'list'     => collect($paginator->items())->map($closure),
            'pageNum'  => $paginator->currentPage(),
            'pageSize' => $paginator->perPage(),
            'count'    => $paginator->total(),
        ];
    }
}
